<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/11/14
 * Time: 15:02
 */

namespace api\modules\other\controllers;


use common\components\che300\Config;
use common\components\sms\Lhwt;
use common\components\tools\Model;
use common\components\tools\Tools;
use common\models\Che300CarModel;
use common\models\Che300CmVinBind;
use common\models\FileListInfo;
use function GuzzleHttp\Psr7\str;
use wodrow\yii\rest\ApiException;
use wodrow\yii\rest\Controller;
use yii\httpclient\Client;

class DefaultController extends Controller
{
    /**
     * 获取ICBC专项卡
     * @desc get/post
     * @param int $guarantee_company_id 公司id
     * @param int $datestart 开始时间
     * @param int $dateend 结束时间
     * @throws
     * @return array
     * @return array list 数据列表
     */
    public function actionGetIcbcSpecialCard($guarantee_company_id = 1, $datestart = 20181112, $dateend = 20181114)
    {
        $r = [];
        $sql = <<<SQL
SELECT `name`, cert_no, card_no, datadt FROM file_list_info
LEFT JOIN file_list_notice ON file_list_notice.id = file_list_info.notice_id
LEFT JOIN bank_guarantee_company_bind ON bank_guarantee_company_bind.assurerno = file_list_info.guaranty_unit_no
WHERE `type` = '0' AND LENGTH(card_no) = 16
AND bank_guarantee_company_bind.guarantee_company_id = {$guarantee_company_id} -- 作为参数
AND datadt BETWEEN {$datestart} AND {$dateend} -- 作为参数
SQL;
        $data = \Yii::$app->db->createCommand($sql)->query();
        $r['list'] = $data;
        return $r;
    }

    /**
     * 获取文件列表
     * @desc get/post
     * @param string $date YYYYMMDD
     * @throws
     * @return array
     * @return array list 数据列表
     */
    public function actionGetFileListInfo($date = null)
    {
        if (!$date){
            $date = FileListInfo::find()->orderBy(['data_date' => SORT_DESC])->where(['type' => 1, 'guaranty_unit_no' => 486])->one();
        }
        $list = FileListInfo::find()->select([
            'name', 'cert_no', 'card_no', 'data_date', 'card_balance', 'optimal_repayment_amout', 'cumulative_break_contract_times', 'continuity_break_contract_times',
        ])->where(['data_date' => $date, 'type' => 1, 'guaranty_unit_no' => 486])->asArray()->all();
        $r['list'] = $list;
        return $r;
    }

    /**
     * 发送短信
     * @desc get/post
     * @param string $mobile
     * @param string $content
     * @return array
     * @return int r 0:发送成功
     */
    public function actionSendSms($mobile, $content)
    {
        $mobile = trim($mobile);
        $content = trim($content);
        $sms = new Lhwt();
        $r = $sms->sendSms([$mobile], $content);
        return [
            'r' => $r,
        ];
    }
}